package com.cskaoyan.shiro;

import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketRoleMapper;
import com.cskaoyan.mapper.RolePermissionsRelationMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * 创建日期: 2022/04/10 23:10
 *
 * @author HanWeiqi
 */
@Component
public class AdminRealm extends AuthorizingRealm {
    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Autowired
    RolePermissionsRelationMapper rolePermissionsRelationMapper;

    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        MallToken token = (MallToken) authenticationToken;
        String username = token.getUsername();

        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike(username);
        }
        // 由于用户名唯一，所以如果输入用户名存在，那么这个list只有一条数据
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(marketAdminExample);
        // 没有查询到这个用户名，直接返回null
        if (marketAdmins == null || marketAdmins.size() == 0) {
            return null;
        }
        MarketAdmin marketAdmin = marketAdmins.get(0);
        System.out.println(this.getName());
        // 得到查询出的用户的密码
        String password = marketAdmin.getPassword();
        return new SimpleAuthenticationInfo(marketAdmin, password, this.getName());
    }

    //获得授权信息
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //授权方法中可以拿到用户信息
        MarketAdmin marketAdmin = (MarketAdmin) principalCollection.getPrimaryPrincipal();
        // 拿到管理员的角色id
        Integer[] roleIds = marketAdmin.getRoleIds();
        Set<String> permissions = new HashSet<>();
        Set<String> roles = new HashSet<>();

        // 根据角色id获取角色名
        // 根据角色id获得角色授权
        for (Integer roleId : roleIds) {
            List<String> results = rolePermissionsRelationMapper.selectPermissionsByRoleId(roleId);
            MarketRole marketRole = marketRoleMapper.selectByPrimaryKey(roleId);
            roles.add(marketRole.getName());
            permissions.addAll(results);
        }

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setRoles(roles);
        simpleAuthorizationInfo.setStringPermissions(permissions);

        return simpleAuthorizationInfo;
    }
}
